package com.hddz.comlib.ui.base;

import android.content.Context;
import android.content.res.TypedArray;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.hddz.comlib.R;

public class EditCancel extends LinearLayout implements EdtInterface {

	private TextView tv;
	private ImageButton ib;
	private EditText et;
	private ImageView iv;

	public EditCancel(Context context) {
		super(context);

	}

	public EditCancel(Context context, AttributeSet attrs) {
		super(context, attrs);
		LayoutInflater.from(context).inflate(R.layout.view_editcancel, this, true);

		TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.EditCancel);
		tv = (TextView) findViewById(R.id.tv);
		iv = (ImageView) findViewById(R.id.iv);
		ib = (ImageButton) findViewById(R.id.ib);
		et = (EditText) findViewById(R.id.et);
		if (typedArray.getString(R.styleable.EditCancel_hind) != null) {
			et.setHint(typedArray.getString(R.styleable.EditCancel_hind));
		}
		tv.setTextAppearance(context, typedArray.getResourceId(R.styleable.EditCancel_tv_style, R.style.text_style_1));
		tv.setText(typedArray.getString(R.styleable.EditCancel_tv_text));
		if (typedArray.getInt(R.styleable.EditCancel_tv_visibility, View.GONE) == View.GONE) {
			tv.setVisibility(View.GONE);
		} else {
			RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) tv.getLayoutParams();
			params.setMargins(params.leftMargin, params.topMargin, params.rightMargin + 15, params.bottomMargin);
			tv.setLayoutParams(params);
		}
		et.addTextChangedListener(tw);// 为输入框绑定一个监听文字变化的监听器
		et.setTextAppearance(context, typedArray.getResourceId(R.styleable.EditCancel_et_style, R.style.text_style_1));
		// 添加按钮点击事件
		ib.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				hideBtn();// 隐藏按钮
				et.setText("");// 设置输入框内容为空
			}
		});
		iv.setImageResource(typedArray.getResourceId(R.styleable.EditCancel_iv_res, android.R.drawable.ic_search_category_default));
		iv.setVisibility(typedArray.getInt(R.styleable.EditCancel_iv_visibility, View.VISIBLE));
	}

	// 当输入框状态改变时，会调用相应的方法
	TextWatcher tw = new TextWatcher() {

		@Override
		public void onTextChanged(CharSequence s, int start, int before, int count) {
			// TODO Auto-generated method stub

		}

		@Override
		public void beforeTextChanged(CharSequence s, int start, int count, int after) {
			// TODO Auto-generated method stub

		}

		// 在文字改变后调用
		@Override
		public void afterTextChanged(Editable s) {
			if (s.length() == 0) {
				hideBtn();// 隐藏按钮
			} else {
				showBtn();// 显示按钮
			}
		}

	};

	public Editable getText() {
		return et.getText();
	}

	@Override
	public void hideBtn() {
		// 设置按钮不可见
		if (ib.isShown())
			ib.setVisibility(View.GONE);

	}

	@Override
	public void showBtn() {
		// 设置按钮可见
		if (!ib.isShown())
			ib.setVisibility(View.VISIBLE);

	}
}

interface EdtInterface {

	public void hideBtn();

	public void showBtn();

}
